package example.controller;


import com.github.pagehelper.PageInfo;
import example.entity.Goods;
import example.entity.Goodstype;
import example.service.GoodsService;
import example.service.impl.GoodsServiceImpl;
import example.service.GoodstypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

@Api("商品控制器")
@Controller
@RequestMapping("/admin")
public class GoodsCtrl {
    @Resource
    GoodsServiceImpl goodsService;

    @Resource
    GoodstypeService goodstypeService;

    @RequestMapping(value = "/goods")
    public String getGoodsList(Model model,
                               @RequestParam(value = "pageNum",required = false,defaultValue ="1" )Integer pageNum,
                               @RequestParam(value = "pageSize",required = false,defaultValue = "3")Integer pageSize) {
       PageInfo<Goods> goodListPage = goodsService.queryAll(pageNum,pageSize);

        model.addAttribute("goodsListPage", goodListPage);
        return "goodsList";
    }
    @ApiOperation("根据ID删除商品")
    @GetMapping("delGoods/{id}")
    public String delGoods(@PathVariable("id") int id ,Model model) {
         int num = goodsService.deleteById(id);
         model.addAttribute("msg","删除成功！");
         return  "forward:/admin/goods";

    }
    // 通过request对象实现转发
//    @GetMapping("/delGoods/{id}")
//    public void delGoods(@PathVariable("id") int id, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        int num = goodsService.deleteById(id);
//        request.setAttribute("msg","删除成功！");
//        request.getRequestDispatcher("/goods").forward(request,response);
//    }

    // 通过ModelAndView对象实现转发
//    @GetMapping("/delGoods/{id}")
//    public ModelAndView delGoods(@PathVariable("id") int id) {
//        int num = goodsService.deleteById(id);
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("msg","删除成功！");
//        modelAndView.setViewName("forward:/goods");
//        return modelAndView;
//    }



    @GetMapping("/goods/{id}")
    public String getGoodsById(@PathVariable("id") int id, Model model) {
        Goods goods = goodsService.findById(id);
        model.addAttribute("goods", goods);
        ArrayList<Goodstype> goodstypesList = goodstypeService.queryAll();
        model.addAttribute("goodstypesList", goodstypesList);
        return "goodsEdit";
    }



    @PostMapping("/editGoods")
    public String editGoods(Goods goods,Model model){
        System.out.println(goods);
        int num = goodsService.update(goods);
        model.addAttribute("msg","商品信息过更新成功！");
        //        ArrayList<Goodstype> goodstypesList = goodstypeService.queryAll();
//        model.addAttribute("goodstypesList",goodstypesList);
        return "forward:/admin/goods";
    }


    @GetMapping("/gotoGoodsAdd")
    public String gotoGoodsAdd(Model model){
        ArrayList<Goodstype> list = goodstypeService.queryAll();
        model.addAttribute("goodstypesList",list);
        return "goodsAdd";
    }
    @PostMapping("/addGoods")
    public String addGoods(Goods goods,Model model){
        int num = goodsService.insert(goods);
        model.addAttribute("msg","商品添加成功");
        return "forward:/admin/goods";
    }
    @RequestMapping("/test")
        public Goods test(){return  new Goods();}
}

